//
//  NewsViewController.m
//  MK
//
//  Created by Le Quang Vinh on 8/10/12.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#import "NewsViewController.h"

@interface NewsViewController ()

@end

@implementation NewsViewController

@synthesize tableView;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        UITabBarItem *item = [[UITabBarItem alloc] initWithTitle:@"News" image:[UIImage imageNamed:@"news.png"] tag:2];
        self.tabBarItem = item;
        [item release];
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.title = @"News";
    if (_refreshHeaderView == nil) {
		
		EGORefreshTableHeaderView *view = [[EGORefreshTableHeaderView alloc] initWithFrame:CGRectMake(0.0f, 0.0f - self.tableView.bounds.size.height, self.view.frame.size.width, self.tableView.bounds.size.height)];
		view.delegate = self;
		[self.tableView addSubview:view];
		_refreshHeaderView = view;
		[view release];
		
	}
	//  update the last update date
	[_refreshHeaderView refreshLastUpdatedDate];
    // Do any additional setup after loading the view from its nib.
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

#pragma mark -
#pragma mark UITableViewDataSource

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 96;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 10;
}

- (UITableViewCell *)tableView:(UITableView *)tView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    /*
    static NSString *CellIdentifier = @"Cell";
    
    UITableViewCell *cell = [tView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    }
    
	// Configure the cell.
    
    return cell;
     */
    UILabel *textLabel;
    UILabel *timeLabel;
    UILabel *detailLabel;
    UIImageView *borderImv;
    UIImageView *pictureImageView;
    
	UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
	if (cell == nil) 
	{
		cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"Cell"] autorelease];
        
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        
        cell.backgroundColor = [UIColor clearColor];
        
        cell.accessoryType = UITableViewCellAccessoryNone;
        
        textLabel = [[UILabel alloc] initWithFrame:CGRectMake(96, 15, 210, 30)];
        [textLabel setTag:100];
		[textLabel setBackgroundColor:[UIColor clearColor]];
		[textLabel setFont:[UIFont boldSystemFontOfSize:16]];
        textLabel.shadowOffset = CGSizeMake(-0.3, 0);
        textLabel.shadowColor = [UIColor darkGrayColor];
		[textLabel setTextColor:[UIColor grayColor]];
        [cell addSubview:textLabel];
        [textLabel release];
        
        timeLabel = [[UILabel alloc] initWithFrame:CGRectMake(96, 37, 280, 30)];
        [timeLabel setTag:101];
		[timeLabel setBackgroundColor:[UIColor clearColor]];
		[timeLabel setFont:[UIFont systemFontOfSize:12]];
		[timeLabel setTextColor:[UIColor colorWithWhite:0.0 alpha:0.75]];
        [cell addSubview:timeLabel];
        [timeLabel release];
        
        detailLabel = [[UILabel alloc] initWithFrame:CGRectMake(96, 51, 280, 30)];
        [detailLabel setTag:103];
		[detailLabel setBackgroundColor:[UIColor clearColor]];
		[detailLabel setFont:[UIFont systemFontOfSize:12]];
		[detailLabel setTextColor:[UIColor colorWithWhite:0.0 alpha:0.75]];
        [cell addSubview:detailLabel];
        [detailLabel release];
        
        borderImv = [[UIImageView alloc] initWithFrame:CGRectMake(7, 6, 75, 75)];
        borderImv.backgroundColor = [UIColor clearColor];
        [borderImv setImage:[UIImage imageNamed:@"adele.jpg"]];
        [cell addSubview:borderImv];
        [borderImv release];
        
        UIImageView *indicatorImv = [[UIImageView alloc] initWithFrame:CGRectMake(300, 41, 10, 10)];
        [indicatorImv setImage:[UIImage imageNamed:@"add.png"]];
        [cell addSubview:indicatorImv];
        [indicatorImv release];
        
        pictureImageView = [[UIImageView alloc] initWithFrame:CGRectMake(12, 12, 71, 71)];
        pictureImageView.backgroundColor = [UIColor clearColor];
        
        pictureImageView.tag = 102;
        [cell addSubview:pictureImageView];
        [pictureImageView release];
	}
	
    textLabel = (UILabel*)[cell viewWithTag:100];
    timeLabel = (UILabel*)[cell viewWithTag:101];
    pictureImageView = (UIImageView*)[cell viewWithTag:102];
    detailLabel = (UILabel*)[cell viewWithTag:103];
    

    textLabel.text = @"SOMEONE YOU LIKE";
    timeLabel.text =  @"6/04/2012 ";
    detailLabel.text =  @"ADELE";
    return cell;
}
//
//- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
//	
//	return [NSString stringWithFormat:@"Section %i", section];
//	
//}


#pragma mark -
#pragma mark Data Source Loading / Reloading Methods

- (void)reloadTableViewDataSource{
	
	//  should be calling your tableviews data source model to reload
	//  put here just for demo
	_reloading = YES;
	
}

- (void)doneLoadingTableViewData{
	
	//  model should call this when its done loading
	_reloading = NO;
	[_refreshHeaderView egoRefreshScrollViewDataSourceDidFinishedLoading:self.tableView];
	
}


#pragma mark -
#pragma mark UIScrollViewDelegate Methods

- (void)scrollViewDidScroll:(UIScrollView *)scrollView{	
	
	[_refreshHeaderView egoRefreshScrollViewDidScroll:scrollView];
    
}

- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{
	
	[_refreshHeaderView egoRefreshScrollViewDidEndDragging:scrollView];
	
}


#pragma mark -
#pragma mark EGORefreshTableHeaderDelegate Methods

- (void)egoRefreshTableHeaderDidTriggerRefresh:(EGORefreshTableHeaderView*)view{
	
	[self reloadTableViewDataSource];
	[self performSelector:@selector(doneLoadingTableViewData) withObject:nil afterDelay:3.0];
	
}

- (BOOL)egoRefreshTableHeaderDataSourceIsLoading:(EGORefreshTableHeaderView*)view{
	
	return _reloading; // should return if data source model is reloading
	
}

- (NSDate*)egoRefreshTableHeaderDataSourceLastUpdated:(EGORefreshTableHeaderView*)view{
	
	return [NSDate date]; // should return date data source was last changed
	
}


@end
